/*
 * NvsClient.cpp
 *
 *  Created on: 2018年8月2日
 *      Author: zhengboyuan
 */

#include "stdafx.h"
#include "NvsClient.h"
#include "TSafeStr.h"
#include "CLog.h"


void NvsClient::Init()
{
	MDecode_Init();
}

void NvsClient::Quit()
{
	MDecode_Uninit();
}


NvsClient::NvsClient():
        m_system(),
		m_shouldLogout()
{
    m_system = NVS_create();
    m_system->init();
}

NvsClient::~NvsClient()
{
    if (m_system)
    {
        m_system->uninit();

        NVS_destroy(m_system);
        m_system = NULL;
    }
}

bool NvsClient::Login(const char* ip, int port, const char* username, const char* password)
{
    nvs::PlatformInfo info = nvs::PlatformInfo();
    comn::copyStr(info.ip, ip);
    info.port = port;
    comn::copyStr(info.username, username);
    comn::copyStr(info.password, password);

    int ret = m_system->login(info);
	bool done = (ret == nvs::NVS_ERR_OK);
	m_shouldLogout = true;

	if (!done)
	{
		CLog::warning("login failed. ret: %d\n", ret);
	}

    return done;
}

void NvsClient::Logout()
{
	if (!m_system)
	{
		return;
	}

	if (m_shouldLogout)
	{
		m_system->logout();
		m_shouldLogout = false;
	}
    
}

bool NvsClient::IsLogin()
{
    return m_system->isLogin();
}

int NvsClient::SetParam(const char* name, const char* value)
{
    int ret = m_system->setParam(name, value);
	return ret;
}

int NvsClient::ControlPtz(const char* id, int cmd, int speed, int data)
{
    int ret = m_system->controlPtz(id, cmd, speed, data);
	return ret;
}

nvs::NetVideoSystem* NvsClient::getVideoSystem()
{
	return m_system;
}